package com.taobao.askwho.ao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import com.taobao.askwho.db.impl.TagInfoDao;
import com.taobao.askwho.po.TagMemory;
import com.taobao.askwho.po.TagRelation;

public class TagAo {
	Logger logger = Logger.getLogger(TagAo.class);
	public TagAo(){}
	private static final TagAo tagAo = new TagAo();
	public static TagAo get() {
		return tagAo;
	}
	private TagInfoDao tagInfoDao = new TagInfoDao();
	public void addTagRelation(TagRelation tagRelation) throws SQLException {
		tagInfoDao.addTagRelation(tagRelation);
	}
	
	public void deleteTagRelationByUuid(Set<String> uuidSet) throws SQLException {
		tagInfoDao.deleteTagRelationByUuid(uuidSet);
	}
	
	public void deleteUrlByTagUuid(Set<String> uuidSet) throws SQLException {
		tagInfoDao.deleteUrlByTagUuid(uuidSet);
	}
	
	public void deletePersonByTagUuid(Set<String> uuidSet) throws SQLException {
		tagInfoDao.deletePersonByTagUuid(uuidSet);
	}
	
	/**
	 * 返回所有的tag及相应的总数
	 * @return
	 */
	public Map<String, Long> queryAllTaqsFromTagRelation() {
		//暂时写为10000
		Map<String, Long> map = tagInfoDao.queryAllTaqsFromTagRelation(0, Integer.MAX_VALUE);
		Map<String, Long> finalMap = new HashMap<String, Long>(); 
		for(Entry<String,Long> entry : map.entrySet()) {
			try {
				String[] array = entry.getKey().split(",");
				for (String keyName : array) {
					Long value = finalMap.get(keyName);
					if (value == null) {
						finalMap.put(keyName, new Long(1));
					} else {
						finalMap.put(keyName, finalMap.get(keyName) + value);
					}
				}
			} catch (Exception e) {
				logger.error("", e);
			}
		}
		return finalMap;
	}

    public Map<String, Long> queryAllTaqsFromMemory() {
        //暂时写为10000
        return  tagInfoDao.queryAllTaqsFromMemory(0, Integer.MAX_VALUE);
    }
	
	public List<TagRelation> queryForLuceneIndex() {
		return tagInfoDao.queryForLuceneIndex(0, Integer.MAX_VALUE);
	}
	
	public List<TagMemory> listTagRelation() {
		try {
			return tagInfoDao.listTagRelation();
		} catch (Exception e) {
			logger.error("", e);
		}
		return new ArrayList<TagMemory>();
	}
	
	public List<TagMemory> queryTagMemoryListLike(String queryString) {
		try {
			//默认查询前100
			return tagInfoDao.queryTagMemoryListLike(queryString, 15);
		} catch (Exception e) {
			logger.error("", e);
		}
		return new ArrayList<TagMemory>();
	}
	
	public boolean addTagMemory(TagMemory po) {
		try {
			tagInfoDao.addTagMemory(po);
		} catch (Exception e) {
			logger.error("", e);
			return false;
		}
		return true;
	}
	
	public boolean deleteTagMemoryByTagName(String tagName) {
		try {
			tagInfoDao.deleteTagMemoryByTagName(tagName);
		} catch (Exception e) {
			logger.error("", e);
			return false;
		}
		return true;		
	}
	public boolean deleteTagMemoryAll() {
		try {
			tagInfoDao.deleteTagMemoryAll();
		} catch (Exception e) {
			logger.error("", e);
			return false;
		}
		return true;		
	}
	public boolean addTagMemoryNoCheck(TagMemory po) {
		try {
			tagInfoDao.addTagMemoryNoCheck(po);
		} catch (Exception e) {
			logger.error("", e);
			return false;
		}
		return true;	
	}


}
